home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / bluebook.zip / FILES.ASM < prev    next >
Assembly Source File  |  1986-05-11  |  12KB  |  386 lines

  1.                                      COMMENT ~
  2. FILES.ASM -- File manipulation Procedures
  3.  
  4.    From `BLUEBOOK of ASSEMBLY ROUTINES for the IBM PC & XT'
  5.          by Christopher L. Morgan
  6.          Copyright (C) 1984 by The Waite Group, Inc.
  7.  
  8.    Contents:
  9.    ---------
  10.    CBUF_CLR    --  Clear the circular buffer
  11.    CBUF_IN    --  Input to circular buffer 
  12.    CBUF_INIT    --  Initialize a circular buffer
  13.    CBUF_OUT    --  Output to a circular buffer
  14.    CBUF_PUT    --  Put a byte into a circular buffer
  15.    CLOS_FIL    --  Close a file
  16.    EMSG_OUT    --  Exception message output
  17.    FILT_CR*    --  Filter for carriage return/linefeed
  18.    MAKE_FIL    --  Create a file
  19.    READ_FIL    --  Read bytes from a file
  20.    SAVE        --  Save a file from communications line to disk
  21.    WRIT_FIL    --  Write bytes to a file
  22.  
  23.     >>>>>  See FILES.DOC for complete descriptions of these routines. <<<<<
  24.  ------------------------------------------------------------------------------
  25.   It is better to put this data in the source code calling these routines, and
  26.     then comment out this section.                          ~
  27. DATAS    SEGMENT    PUBLIC
  28.     CR    EQU    0DH            ;ASCII code for carriage return
  29.     LF    EQU    0AH            ;ASCII code for linefeed
  30.     CTRL_Z    EQU    1AH            ;ASCII code for ^Z
  31.     BLK_SIZ   =    0            ;K=block size
  32.     CBUF_SIZ      DW    256             ;K=CBUF max capacity
  33.     CBUF         DB    1        ;K=base address of CBUF
  34.     LBUF        DB    256 DUP('LBUF')
  35.     NAME_BUF    DB    64 DUP('NAME')
  36. ;
  37. ;Following table used by EMSG_OUT
  38.     EMSG    DW    EMSG1
  39.         DW    EMSG2
  40.         DW    EMSG3
  41.         DW    EMSG4
  42.         DW    EMSG5
  43.         DW    EMSG6
  44.         DW    EMSG7
  45.         DW    EMSG8
  46.         DW    EMSG9
  47.         DW    EMSG10
  48.         DW    EMSG11
  49.         DW    EMSG12
  50.         DW    EMSG13
  51.         DW    EMSG14
  52.         DW    EMSG15
  53.         DW    EMSG16
  54.         DW    EMSG17
  55.         DW    EMSG18
  56.     EMSG1    DB    CR,LF,'Invalid function number',CR,LF,0 
  57.     EMSG2    DB    CR,LF,'File not found',CR,LF,0
  58.     EMSG3    DB    CR,LF,'Path not found',CR,LF,0
  59.     EMSG4    DB    CR,LF,'Too many open files',CR,LF,0 
  60.     EMSG5    DB    CR,LF,'Access denied',CR,LF,0
  61.     EMSG6    DB    CR,LF,'Invalid handle',CR,LF,0
  62.     EMSG7    DB    CR,LF,'Memory control blocks destroyed',CR,LF,0
  63.     EMSG8    DB    CR,LF,'Insufficient memory',CR,LF,0
  64.     EMSG9    DB    CR,LF,'Invalid memory block address',CR,LF,0
  65.     EMSG10  DB    CR,LF,'Invalid environment',CR,LF,0
  66.     EMSG11    DB    CR,LF,'Invalid format',CR,LF,0
  67.     EMSG12    DB    CR,LF,'Invalid access code',CR,LF,0
  68.     EMSG13    DB    CR,LF,'Invalid data',CR,LF,0
  69.     EMSG14    DB    CR,LF,'Message not in use',CR,LF,0
  70.     EMSG15    DB    CR,LF,'Invalid drive was specified',CR,LF,0
  71.     EMSG16    DB    CR,LF,'Attempted to remove the current directory',CR,LF,0
  72.     EMSG17    DB    CR,LF,'Not same device',CR,LF,0
  73.     EMSG18    DB    CR,LF,'No more files',CR,LF,0
  74. DATAS    ENDS 
  75. ;------------------------------------------------------------------------------
  76. ;It is better to include this data in the source code calling these routines, 
  77. ;  and then comment out this section.        
  78. STACKS    SEGMENT    STACK
  79.     DB    20 DUP(' STACKS ')
  80. STACKS    ENDS
  81. ;------------------------------------------------------------------------------
  82. CODES    SEGMENT
  83. ;The following variables/constants must be defined in the source code calling
  84. ;  these routines. See FILES.DOC.
  85.  
  86.     EXTRN    COM_INCK:FAR,COM_INIT:FAR,COM_ON:FAR,COM_OFF:FAR,MSG_OUT:FAR
  87.     EXTRN    DTA:DWORD,LAST:BYTE,GAUGE:WORD,NEARFULL:WORD,NEAREMPTY:BYTE
  88.  
  89.     PUBLIC    EMSG_OUT,GET_SPEC,MAKE_FIL,CLOS_FIL,WRIT_FIL,READ_FIL
  90.     PUBLIC    CBUF_INIT,CBUF_PUT,CBUF_IN,CBUF_CLR,CBUF_OUT,SAVE
  91.  
  92. ASSUME CS:CODES,DS:DATAS
  93. ;----------------------------I/O ROUTINES--------------------------------------
  94. ;Routine to send out exception messages
  95. EMSG_OUT    PROC    FAR
  96.     PUSH    SI                ;Save registers
  97.     PUSH    AX
  98.     ADD    AX,AX                ;Double to index through table
  99.     MOV    SI,AX                ;SI points into table
  100.     MOV    SI,EMSG[SI]            ;Look up address of message
  101.     CALL     MSG_OUT                ;Send the message
  102.     POP    AX                ;Restore registers
  103.     POP    SI
  104.     RET
  105. EMSG_OUT    ENDP
  106. ;------------------------------------------------------------------------------
  107. ;Routine to get a file specifier 
  108. ;
  109. GET_SPEC    PROC    FAR
  110.     PUSH    DS                ;Save registers
  111.     PUSH    ES
  112.     PUSH    SI
  113.     PUSH    DI
  114.     PUSH    CX
  115. ;
  116. ;Set up pointer to DTA    to get parameters
  117.     LDS    SI,DTA                ;Point to DTA for parameters
  118.     MOV    CL,[SI]                ;Get length of string
  119.     MOV    CH,0                ;Make 16-bit
  120.     INC    SI                ;Skip the length byte
  121.     MOV    AL,' '                ;Scan past the spaces    
  122. GET_SPEC1:
  123.     CMP    [SI],AL                ;Check for space
  124.     JNE    GET_SPEC2            ;Exit loop if nonspace
  125.     INC    SI                ;Else point to next byte
  126.     LOOP    GET_SPEC1            ;Loop back for more
  127.     JCXZ    GET_SPEC3            ;No file specifier?
  128. ;
  129. ;Move the rest into place
  130. GET_SPEC2:
  131.     MOV    DI,DX                ;Index points to destination
  132.     CLD                    ;Forward direction
  133.     REP    MOVSB                ;Make the transfer
  134.     CLC                    ;No error, so no carry
  135.     JMP    GET_SPEC_XIT            ; and return
  136. GET_SPEC3:
  137.     MOV    AX,20                ;No file specified
  138.     STC                    ;Set carry for error
  139.     JMP    GET_SPEC_XIT            ; and exit
  140. GET_SPEC_XIT:
  141.     POP    CX                ;Restore registers
  142.     POP    DI
  143.     POP    SI
  144.     POP    ES
  145.     POP    DS
  146.     RET
  147. GET_SPEC    ENDP
  148. ;------------------------------------------------------------------------------
  149. ;Routine to create a file
  150. ;
  151. MAKE_FIL    PROC    FAR
  152.     PUSH    CX                ;Save register
  153.     MOV    CX,0                ;Attribute 0
  154.     INT    21H                ;DOS call
  155.     POP    CX                ;Restore register
  156.     RET
  157. MAKE_FIL    ENDP
  158. ;------------------------------------------------------------------------------
  159. ;Routine to close a file 
  160. ;
  161. CLOS_FIL    PROC    FAR
  162.     MOV    AH,3EH                ;Close a file
  163.     INT    21H                ;DOS call
  164.     RET
  165. CLOS_FIL    ENDP
  166. ;------------------------------------------------------------------------------
  167. ;Routine to write bytes to a file
  168. ;
  169. WRIT_FIL    PROC    FAR
  170.     MOV    AH,40H                ;Write to a file
  171.     INT    21H                ;DOS call
  172.     CMP    AX,CX                ;Was it all written?
  173.     JE    WRIT_FIL_XIT            ;Skip if yes
  174.     MOV    AX,21                ;Not all bytes were transferred
  175.     STC                    ;Set carry for error
  176. WRIT_FIL_XIT:
  177.     RET
  178. WRIT_FIL    ENDP
  179. ;------------------------------------------------------------------------------
  180. ;Routine to read bytes from a file
  181. ;
  182. READ_FIL    PROC    FAR
  183.     MOV    AH,3FH                ;Read from a file
  184.     INT    21H                ;DOS call
  185.     CMP    AX,CX                ;Everything back?
  186.     JE    READFIL_XIT            ;Skip if yes
  187.     MOV    AX,22                ;Not all read
  188.     STC                    ;Set carry for error
  189. READFIL_XIT:
  190.     RET
  191. READ_FIL    ENDP
  192. ;------------------------------------------------------------------------------
  193. ;Routine to handle input for a circular buffer 
  194. ;
  195. CBUF_INIT    PROC    FAR
  196.     MOV    SI,0                ;Initialize SI
  197.     MOV    DI,0                ;Initialize DI
  198.     MOV    GAUGE,0                ; and # of bytes in buffer
  199.     MOV    LAST,0                ; and last byte buffer
  200.     RET
  201. CBUF_INIT    ENDP
  202. ;------------------------------------------------------------------------------
  203. ;Routine to one byte into a circular buffer 
  204. ;
  205. CBUF_PUT    PROC    FAR
  206.     PUSH    DX                ;Save register
  207.     MOV    DX,GAUGE            ;Put V Guage in DX
  208.     MOV    CBUF[DI],AL            ;Put the byte into the buffer
  209.     INC    DI                ;Adjust pointer to next char
  210.     CMP    DI,CBUF_SIZ            ;Wrap it around?
  211.     JNE    CBUF_PUT1            ;Skip if no wrap
  212.     MOV    DI,0                ;Wraps back to zero
  213. CBUF_PUT1:
  214.     INC    DX                ;Count the character
  215.     CMP    DX,NEARFULL            ;Too many characters?
  216.     JNE    CBUF_PUT_XIT            ;Skip if not
  217.     CALL     COM_OFF                ;Request to stop flow
  218. CBUF_PUT_XIT:
  219.     POP    DX                ;Restore register
  220.     RET
  221. CBUF_PUT    ENDP
  222. ;------------------------------------------------------------------------------
  223. ;Routine to handle input to a circular buffer
  224. ;
  225. CBUF_IN    PROC    FAR
  226.     CALL    COM_INCK            ;Check for a character
  227.     JZ    CBUF_IN_XIT            ;Good exit if none
  228.     AND    AL,7FH                ;Strip off parity bit
  229.     CMP    AL,10                ;Check for <LF> (ASCII 10)
  230.     JNE    CBUF_IN1            ;Skip if not <LF>
  231.     CMP    LAST,13                ;Check for <CR> (ASCI 13)
  232.     JE    CBUF_IN2            ;Skip if CR/LF
  233. CBUF_IN1:
  234.     CALL    CBUF_PUT            ;Put byte into buffer
  235.     MOV    LAST,AL                ;Update last byte
  236. CBUF_IN2:
  237.     CMP    AL,13                ;Check for <CR>
  238.     JNE    CBUF_IN3            ;Skip if not
  239. ;
  240. ;Insert LF if character was <CR>
  241.     PUSH    AX                ;Save the current character
  242.     MOV    AL,10                ;<LF>
  243.     CALL    CBUF_PUT            ; goes into buffer
  244.     POP    AX                ;Restore current character
  245. ;
  246. ;Check for end of file
  247. CBUF_IN3:
  248.     CMP    AL,CTRL_Z            ;Check for ^-Z (1AH)
  249.     JE    CBUF_IN5            ;EOF is an exception
  250. ;
  251. ;Check for overflow
  252.     PUSH    DX                ;Save register
  253.     MOV    DX,GAUGE            
  254.     CMP    DX,NEARFULL            ;Overflow of characters?
  255.     JL    CBUF_IN4            ;Skip if not
  256.     CALL    COM_OFF                ;Stop flow request
  257.     JMP    CBUF_IN_XIT            ;Good exit
  258. CBUF_IN4:                    ;No overflow
  259.     CALL    COM_ON                ;Make sure flow is on
  260.     JMP    CBUF_IN_XIT            ;Good exit
  261. CBUF_IN5:                    ;EOF exception handling
  262.     MOV    AX,19                ;EOF exception code
  263.     STC                    ;Set carry